package com.niit.configurer;

import com.niit.interceptor.SessionInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
/**
 * 添加拦截器
 */
public class SessionConfiguration extends WebMvcConfigurerAdapter {
        @Override
        public void addInterceptors(InterceptorRegistry registry){
            registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/**");
            //网站配置生成器：添加一个拦截器，拦截路径为整个项目
            //登录请求不拦截
            registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/**").excludePathPatterns(
                    "/login"
                    ,"/register",
                    "http://localhost:8081/templates/Login.html",
                    "/Login.html"
            );
        }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        //允许跨域访问资源定义
        registry.addMapping("/**")
                //(只允许本地的指定端口访问)允许所有
                .allowedOrigins("http://localhost:8081")
                // 允许发送凭证: 前端如果配置改属性为true之后，则必须同步配置
                .allowCredentials(true)
                // 允许所有方法
                .allowedMethods("*")

                .allowedHeaders("*");
    }

}

